<?php

require_once ROOT_DIR.'models/Todo.php';
require_once ROOT_DIR.'models/Workinterval.php';
require_once ROOT_DIR.'controls/RightHelper.php';

/**
 * Retrieves all workintervals for the given todo id and echos them.
 * @param int $todoID
 */
function getWorkintervals($todoID, $userID){
	
	if(!is_null($todoID))$todo = Todo::getTodo($todoID);
	else if(is_null($userID)) $userID = $_SESSION['USER_ID'];
	
	if(is_null($todoID) || !is_null($todo)){
		if($_SESSION['USER_NAME'] == 'admin' || $userID == $_SESSION['USER_ID'] || (!is_null($todo) && ($todo->getCreatorID() == $_SESSION['USER_ID'] || RightHelper::checkIfLinkedToTodo($todo)))){
			$wa = Workinterval::getWorkintervals($todoID, $userID);
			echo WorkintervalJSON::getView($wa);
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_TODO_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>